package com.csust.automaticteach.app.teacher.iflytek;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.csust.automaticteach.app.teacher.iflytek.ppt.Authentication;
import com.csust.automaticteach.app.teacher.iflytek.ppt.ApiClient;
import com.csust.automaticteach.app.teacher.iflytek.ppt.model.vo.CreateResponse;
import com.csust.automaticteach.model.dto.generateppt.ThemeTemplateQueryRequest;
import org.springframework.web.reactive.function.client.WebClient;

import java.io.File;

/**
 * @ClassNameGenerateMain
 * @Description TODO
 * @Author钰玟
 * @Date2025/7/17 下午11:43
 * @Version 1.0
 **/

public class GenerateMain {
    public static final String APPID = "这里填写控制台的APPID";
    public static final String APISecret = "这里填写控制台的APISecret";

    public static void main(String[] args) throws Exception {
        JSONObject json = new JSONObject();
        json.set("style", "商务");
        json.set("pageSize", "10");
        ThemeTemplateQueryRequest themeTemplateQueryRequest = new ThemeTemplateQueryRequest();
        themeTemplateQueryRequest.setStyle("商务");
        String jsonStr = JSONUtil.toJsonStr(themeTemplateQueryRequest);
        System.out.println(json);
        System.out.println(jsonStr);


//         获取请求头中需要携带的参数 appId（控制台获取）, timestamp（时间戳，单位：秒，与服务端时间相差五分钟之内）, signature（签名）
        long timestamp = System.currentTimeMillis() / 1000;
        String ts = String.valueOf(timestamp);

        Authentication auth = new Authentication();
        String signature = auth.getSignature(APPID, APISecret, timestamp);

        // 建立链接
        ApiClient client = new ApiClient(WebClient.builder());

        // PPT主题列表查询【所有模板均免费使用】
//        String templateResult = client.getTemplateList(APPID, ts, signature);
//        System.out.println(templateResult);

        // 大纲生成 createOutline
        String outlineQuery = "这是一个大纲生成的测试";
        String outlineResp = client.createOutline(APPID, ts, signature, outlineQuery);//query
        System.out.println(outlineResp);

//        大纲生成 createOutlineByDoc
        File outlineFile = new File("src\\main\\resources\\test.txt");//根据文档生成大纲
        String outlineByDoc = client.createOutlineByDoc(APPID, ts, signature, outlineFile);
        System.out.println(outlineByDoc);

        //获取大纲生成接口得到的大纲
        CreateResponse outlineResponse = JSON.parseObject(outlineByDoc, CreateResponse.class);
        String outline = outlineResponse.getData().getOutline();
        System.out.println("生成的大纲如下：" + outline);


        //PPT生成 （通过大纲生成PPT）
        String resp1 = client.createPptByOutline(APPID, ts, signature, outline);
        System.out.println(resp1);

        //PPT生成 （直接生成PPT）
        File pptFile = new File("src\\main\\resources\\test.txt");
        String resp2 = client.create(APPID, ts, signature, pptFile);
        System.out.println(resp2);

        //根据PPT生成接口返回的sid，得到最终PPT的下载链接
        CreateResponse pptResponse = JSON.parseObject(resp1, CreateResponse.class);
        System.out.println("sid=" + pptResponse.getData().getSid());
        String sid = "这里填写PPT生成接口返回的sid";
        String progressResult = client.checkProgress(APPID, ts, signature, sid);
        System.out.println(progressResult);
    }
}